package online.longzipeng.mywebdemo;

import online.longzipeng.mywebdemo.utils.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lzp
 * @Date:2023/2/21
 * @description: 测试时间工具类
 */
public class DateUtilsDemo {

	public static void main(String[] args) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		System.out.printf("当前时间为：%s ==============================\n", df.format(now));
		cal.add(Calendar.SECOND, -20); // 20秒前
		System.out.printf("%s => %s\n", df.format(cal.getTime()), DateUtils.getTimeDescriptionByDate(now, cal.getTime()));
		cal.add(Calendar.MINUTE, -1); // 1分钟前
		System.out.printf("%s => %s\n", df.format(cal.getTime()), DateUtils.getTimeDescriptionByDate(now, cal.getTime()));
		cal.add(Calendar.HOUR, -1); // 1小时前
		System.out.printf("%s => %s\n", df.format(cal.getTime()), DateUtils.getTimeDescriptionByDate(now, cal.getTime()));
		cal.add(Calendar.DAY_OF_MONTH, -1); // 1天前
		System.out.printf("%s => %s\n", df.format(cal.getTime()), DateUtils.getTimeDescriptionByDate(now, cal.getTime()));
		cal.add(Calendar.YEAR, -1); // 1年前
		System.out.printf("%s => %s\n", df.format(cal.getTime()), DateUtils.getTimeDescriptionByDate(now, cal.getTime()));
	}

}
